import { useState, useEffect, useCallback } from 'react';
import request from '@/utils/request';

const getTreeData = (data, fieldsName) =>
    data.map(item => ({
        ...item,
        value: item[fieldsName.value],
        title: item[fieldsName.title],
        key: item[fieldsName.value],
        children: item.children ? getTreeData(item.children, fieldsName) : item.children
    }));

// 获取部门列表
export const useDepartmentList = params => {
    const [loading, setLoading] = useState(false);
    const [treeData, setTreeData] = useState([]);

    useEffect(() => {
        const getData = async () => {
            if (!params.companyId) return;
            setLoading(true);
            const res = await request('/api/org/v1/functional/list', {
                params
            });
            if (res && res.code === 10000 && res.data) {
                setTreeData(
                    getTreeData(res.data, {
                        value: 'id',
                        title: 'departmentName'
                    }) || []
                );
            } else {
                setTreeData([]);
            }
            setLoading(false);
        };

        getData();
    }, [params.companyId]);

    return {
        loading,
        treeData
    };
};
// 获取职位列表
export const useJobList = params => {
    const [loading, setLoading] = useState(false);
    const [treeData, setTreeData] = useState([]);

    useEffect(() => {
        const getData = async () => {
            if (!params.companyId) return;
            setLoading(true);
            const res = await request(`/api/oa/v1/post/getpostlistbycompanyid/${params.companyId}`);
            if (res && res.code === 10000 && res.data) {
                setTreeData(
                    getTreeData(res.data, {
                        value: 'id',
                        title: 'postName'
                    }) || []
                );
            } else {
                setTreeData([]);
            }
            setLoading(false);
        };

        getData();
    }, [params.companyId]);

    return {
        loading,
        treeData
    };
};

// 获取员工列表
export const useEmployeeList = params => {
    const [loading, setLoading] = useState(false);
    const [data, setData] = useState([]);

    const getEmployeeList = useCallback(async innerParams => {
        // console.log(innerParams, 'innerParams');
        // if (!innerParams.userName) {
        //     setData([]);
        //     return;
        // }
        setLoading(true);
        const response = await request('/api/oa/v1/employee/getunderemployeebycondition', {
            method: 'POST',
            data: innerParams
        });
        if (response.code === 10000) {
            setData(response.data || []);
        } else {
            setData([]);
        }
        setLoading(false);
    }, []);
    useEffect(() => {
        getEmployeeList(params);
    }, [getEmployeeList, JSON.stringify(params)]);

    return {
        loading,
        data,
        setData,
        getEmployeeList
    };
};

// 获取公司列表
export const useCompanyList = params => {
    const [loading, setLoading] = useState(false);
    const [companyList, setCompanyList] = useState([]);

    useEffect(() => {
        const getList = async () => {
            setLoading(true);
            const response = await request('/api/org/v1/functional/list', {
                params
            });

            if (response.code === 10000) {
                setCompanyList(
                    getTreeData(response.data, {
                        value: 'id',
                        title: 'companyName'
                    }) || []
                );
            } else {
                setCompanyList([]);
            }
            setLoading(false);
        };

        getList();
    }, []);

    return {
        loading,
        companyList
    };
};

// 获取员工年度涨薪幅度
export const useSalaryincrecords = employeeId => {
    const [records, setRecords] = useState({});

    useEffect(() => {
        const getsalaryincrecords = async () => {
            if (!employeeId) return;
            const response = await request(`/api/oa/v1/salaryincrecords?employeeId=${employeeId}`);
            if (response.code === 10000) {
                setRecords(response.data);
            } else {
                setRecords({});
            }
        };

        getsalaryincrecords();
    }, [employeeId]);

    return {
        records
    };
};
